//
//  LoginVm.m
//  RacLogin
//
//  Created by HB on 2017/10/16.
//  Copyright © 2017年 com.comzenz-service. All rights reserved.
//

#import "LoginVm.h"



@implementation LoginVm

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self commitInit];
    }
    return self;
}

- (void)commitInit {
    
    
    _loginEnableSignal = [RACSignal combineLatest:@[RACObserve(self, name),RACObserve(self, pwd)] reduce:^id (NSString *name, NSString *pwd){
        return @(name.length && pwd.length);
    }];
    
    _loginCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        NSLog(@"发送请求");
        RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
            [mgr GET:@"https://api.douban.com/v2/book/search" parameters:@{@"q":@"美女"} progress:^(NSProgress * _Nonnull downloadProgress) {
                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                
                [subscriber sendNext:responseObject];
                
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
            }];
            
            return nil;
            
        }];
        
        
        return signal;
    }];
    
//    [[_loginCommand execute:nil] subscribeNext:^(id  _Nullable x) {
//        NSLog(@"%@",x);
//    }];
    
//    [_loginCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
//                NSLog(@"%@",x);
//            }];
    
    [[_loginCommand.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
        if ([x boolValue] == YES) {
            NSLog(@"正在执行");
        } else {
            NSLog(@"执行完成");
        }
    }];
    
    
//    _loginEnableSignal = [RACSignal combineLatest:@[RACObserve(self, name),RACObserve(self, pwd)] reduce:^id(NSString *name,NSString *pwd) {
//        return @(name.length && pwd.length);
//    }];
//
//    _loginCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
//        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
//            [subscriber sendNext:@""];
//            [subscriber sendCompleted];
//            return nil;
//        }];
//    }];
//
//    [_loginCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
//        NSLog(@"%@",x);
//    }];
//
//    [[_loginCommand.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
//        if ([x boolValue] == YES) {
//            // 正在执行
//            NSLog(@"正在执行");
//            // 显示蒙版
////            [MBProgressHUD showMessage:@"正在登录ing.."];
////            [MBProgressHUD
//
//        }else{
//            // 执行完成
//            // 隐藏蒙版
////            [MBProgressHUD hideHUDForVie animated:<#(BOOL)#>];
//
//            NSLog(@"执行完成");
//        }
//
//    }];
}

@end
